package cc.deng.wubo.minapp.service.impl;

import cc.deng.wubo.minapp.beans.SysMenu;
import cc.deng.wubo.minapp.core.base.BaseServiceImpl;
import cc.deng.wubo.minapp.service.MenuService;


import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl extends BaseServiceImpl<SysMenu> implements MenuService {
  /*  @Autowired
    private Dao dao;*/
    @Autowired
    public MenuServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public List<SysMenu> findByParentIdAndRoleId(int parentId, int roleId) {
        String sqlStr = "select * from t_menu where id in (select menu_id from t_role_menu where role_id=@roleId) and p_id = @parentId";

        Sql sql = Sqls.create(sqlStr);
        sql.setParam("roleId",roleId);
        sql.setParam("parentId",parentId);
        sql.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection connection, ResultSet rs, Sql sql) throws SQLException {
                List<SysMenu> menus = new ArrayList<>();
                while (rs.next()){
                    SysMenu menu = new SysMenu();
                    menu.setIcon(rs.getString("icon"));
                    menu.setId(rs.getInt("id"));
                    menu.setName(rs.getString("name"));
                    menu.setPid(rs.getInt("p_id"));
                    menu.setUrl(rs.getString("url"));
                    menu.setState(rs.getInt("state"));
                    menus.add(menu);
                }

                return menus;
            }
        });

        this.dao().execute(sql);
        List<SysMenu> menus = sql.getList(SysMenu.class);


        return menus;
    }
}
